﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service;
using Service.Infrastructure;
using Service.Infrastructure.Implement;
using Service.Infrastructure.Interface;
using Service.Infrastructure.Result;
using Web.Controllers;
using Web.Filter;

namespace Web.Areas.Sys.Controllers
{
    [ManagerFilter]
    public class AdminController : Controller
    {
        private readonly ManagerService _managerImpl = new ManagerService();
        private readonly PlatformConfigService _platformConfig = new PlatformConfigService();
        private readonly ExceptionLogConfigService _exceptionLogConfigService = new ExceptionLogConfigService();
        private readonly UserLogService _userLogService = new UserLogService();

        protected readonly IAccount Manager;
        protected readonly INotifications Notifications;
        protected readonly ITask Task;
        protected readonly ISetting Setting;
        protected readonly ILog _exceptionLog;
        protected readonly ILog _userLog;

        public AdminController()
        {
            Task = _managerImpl;
            Notifications = _managerImpl;
            Manager = _managerImpl;
            Task = _managerImpl;
            Setting = _platformConfig;
            _exceptionLog = _exceptionLogConfigService;
            _userLog = _userLogService;
        }

        private void SetTempData(ServiceResult result)
        {
            SetTempData("Data", result);
        }

        private void SetTempData(String key, ServiceResult result)
        {
            TempData[key] = result.ReData;
        }

        //
        // GET: /Sys/Admin/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Users()
        {
            SetTempData(Manager.QueryUsers());
            return View();
        }

        public ActionResult MyProfile()
        {
            return View();
        }

        [ChildActionOnly]
        public PartialViewResult Header()
        {
            SetTempData("NotifData", Notifications.CountNotifications());
            SetTempData("TaskData", Task.PendingTasks());
            SetTempData("AccountInfo", MemoryCache.GetSessionData(_managerImpl, (String)Session[Base.ManagerSessionKey]));
            return PartialView();
        }

        [HttpGet]
        public ActionResult Editor()
        {
            return View();
        }

        [HttpGet]
        public ActionResult PlatformSetting()
        {
            SetTempData(Setting.QueryConfig());
            return View();
        }


        [HttpGet]
        public ActionResult ExceptionLog()
        {
            SetTempData(_exceptionLog.QueryLogs());
            return View();
        }

        [HttpPost]
        public JsonResult HandleException(Guid id)
        {
            return Json(_exceptionLog.ProcessingLog(id));
        }

        [HttpGet]
        public ActionResult UserLog()
        {
            SetTempData(_userLog.QueryLogs());
            return View();
        }

        [HttpPost]
        public JsonResult ChangeConfig()
        {

            var dict = HttpContext.Request.Form.Keys.Cast<string>().ToDictionary(key => key, key => HttpContext.Request.Form[key]);
            return Json(Setting.ChangeConfig(ref dict));
        }

    }
}
